<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Agent;

// use Cache;

class IndexController extends Controller
{
    public function test() {
        $table = DB::table('mytable');
        // dd($table->get());
        $data = $table->get();
        return view('test', compact('data'));
    }

    public function login(Request $request) {

        if($request->isMethod('POST')) {
            $arr = [
                'user'  => 'integer|min:2|max:5',
                'age'   => 'integer|min:2|max:5',
                'email' => 'nullable|email',
                'photo' => 'image|max:304',
            ];
            $request->validate($arr);
            // return '验证通过';
            if($request->hasFile('photo') && $request->photo->isValid()) {
                $path = $request->photo->path();
                echo $path.'<br>';
                $extension = $request->photo->extension();
                echo $extension.'<br>';
                $path2 = $request->photo->store('images', 'public');
                echo $path2.'<br>';
                $path3 = asset("storage/$path2");
                echo $path3.'<br>';
                echo "<img src='$path3' width='300px'>".'<br>';
                return '<br>photo存在，且没出错';
            }
            else {
                return 0000;
            }
        }
        else {
            return view('login');
        }
    }

    //首页框架页面
    public function index() {
        setCache('setting');
        $browser = Agent::browser();
        // return $browser;
        // Agent::isMobile();//设备是手机
        // Agent::isTablet();//设备是平板
        // dd(Agent::isMobile());
        // dd(Agent::isTablet());
        // dd(Agent::isPhone());

        if($browser == "IE") {
            # 如果用户用的是IE浏览器，展示更换浏览器页面
            return view('admin.public.ieError');
        }

        $default_page = "/".config('constant.name_admin')."/index/welcome";

        $auth_A = [];
        $auth_B = [];
        $current_user = auth('admin')->user();
        //超级管理员拥有所有权限
        if($current_user->role_id == 1) {
            $auth_A = DB::table('auth')->where([['level', '0'], ['auth_name', '<>', '']])->orderBy('sort')->get();
            $auth_B = DB::table('auth')->where([['level', '1'], ['auth_name', '<>', '']])->orderBy('sort')->get();
        }
        else {
            //如果不是超级管理员
            //查询当前用户的角色信息
            $auth_ids_str = DB::table('role')->where('id', $current_user->role_id)->value('auth_ids');
            if($auth_ids_str) {
                $arr_auth_ids = explode(',', $auth_ids_str); //字符串 --》数组

                $b = in_array("40", $arr_auth_ids);
                if((Agent::isMobile() || Agent::isTablet()) && in_array("40", $arr_auth_ids)) {
                    # 是手机或平板，而且有结果录入的权限
                    $default_page = "/".config('constant.name_admin')."/contract/indexResLuru";
                }

                $auth_A = \App\Admin\Auth::where([['level', '0'], ['auth_name', '<>', '']])->whereIn('id', $arr_auth_ids)->orderBy('sort')->get();
                $auth_B = \App\Admin\Auth::where([['level', '1'], ['auth_name', '<>', '']])->whereIn('id', $arr_auth_ids)->orderBy('sort')->get();
            }
        }

        $name_site = DB::table('setting')->where('id', '1')->value('name_site');

        $role_ids = explode(',', $current_user->role_ids);
        $roles = DB::table('role')->select('id', 'role_name')->whereIn('id', $role_ids)->get();
        $subTopics = DB::table('auth')->where('sub_topic','!=','')->pluck('sub_topic','id');
        return view('admin.index.index', compact('auth_A', 'auth_B', 'name_site', 'default_page','roles','subTopics'));
    }

    //首页欢迎页面
    public function welcome() {
        /*  phpinfo();
         return; */
        /*  $a = json_encode(["戈文文", 123]);
        $a=json_decode($a,true);
        dd($a); */
        // Cache::put("aaa",  "hello", 10);
        // dd(Cache::get("aaa"));

        $name_site = DB::table('setting')->where('id', '1')->value('name_site');
        $set_sys = getCache('setting');

        $settingdata = $set_sys['warn_days_limit'];
        $expirecount = null;
        if($settingdata) {
            $settingdata = unserialize($settingdata);
            $nexttimelimit = date('Y-m-d', strtotime('+'.$settingdata['days'].' days'));
            $nextMintimelimit = date('Y-m-d', strtotime('-15 days'));
            $expirecount = DB::table('a_sample_info')
                             ->whereDate('next_time', '>', $nextMintimelimit)
                             ->whereDate('next_time', '<', $nexttimelimit)
                             ->where('res', '=', 1)->where('notice', 1)
                             ->count();
        }
        // file_put_contents('view.htm',view('admin.index.welcome', compact('name_site'))->__toString());
        if(config('constant.is_lan')){
            $addr = [];
        }else{
            $addr = curl("http://api.map.baidu.com/location/ip?ak=dELXcqGX0714b9wv6H8f5wsLRyC0mVUN&ip=".request()->getClientIp()."&coor=bd09ll");
        }
    	$agent = new \Jenssegers\Agent\Agent();
    	$isphone = $agent->isPhone();
        $alert_pwd_msg = check_chage_pwd(); //验证密码是否n+个月没改过
        return view('admin.index.welcome', compact('name_site', 'expirecount','set_sys','addr','isphone','settingdata','alert_pwd_msg'));
    }
}